// 计算一个元素所占内存大小

const seen = new WeakMap();

function sizeOfObj(obj) {
    if (obj == null) return 0
    let result = 0;
    const properties = Object.keys(obj)
    for (let i = 0; i < properties.length; i++) {
        const key = properties[i];
        if (typeof obj[key] == 'object' && obj[key] != null) {
            // 可能有重复的 相同指针
            if (seen.has(obj[key])) {
                continue
            }
            seen.add(obj[key])
        }
        result += calculator(key)
        result += calculator(obj[key])
    }
    return result
};

function calculator(obj) {
    const objType = typeof obj;
    switch (objType) {
        case "string":
            return obj.length
        case "boolean":
            return 4
        case "number":
            return 8
        case "object":
            if(Array.isArray(obj)){
                return obj.map(calculator).reduce((acc,curr)=>{
                    return acc + curr
                })
            }else {
                return sizeOfObj(obj)
            }

        default:
            break;
    }
}